package com.example.bbs.ui.fragment;

import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Bundle;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;

import com.example.bbs.R;
import com.example.bbs.ui.activity.BlockActivity;
import com.example.bbs.ui.activity.DraftBoxActivity;
import com.example.bbs.base.Data;
import com.example.bbs.ui.activity.MineBoxActivity;
import com.example.bbs.ui.activity.MyLoginActivity;
import com.example.bbs.ui.activity.MyOwnInfoActivity;
import com.example.bbs.ui.activity.MyRegisterActivity;
import com.example.bbs.ui.activity.NoticeActivity;
import com.example.bbs.ui.activity.PersonalInfo;
import com.example.bbs.ui.view.MeItemView;

import java.util.concurrent.CompletableFuture;

public class HomeFragment extends BaseTabViewPageFragment implements View.OnClickListener{

    static final String LOG_TAG = HomeFragment.class.getSimpleName();
    Button btn_login, btn_register, btn_logout;
    MeItemView mtv_personal_info,me_item_mine,me_item_draft,me_tv_notice,me_tv_block;
    ImageView h_head;

    public static HomeFragment newInstance() {
        return new HomeFragment();
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d(LOG_TAG, "onCreate");
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View rootView = inflater.inflate(R.layout.fragment_home, container, false);
        initView(rootView);
        return rootView;
    }

    public void initView(View v) {
        btn_login = v.findViewById(R.id.btn_login);
        btn_login.setOnClickListener(this);
        btn_register = v.findViewById(R.id.btn_register);
        btn_register.setOnClickListener(this);
        btn_logout = v.findViewById(R.id.btn_log_out);
        btn_logout.setOnClickListener(this);
        mtv_personal_info =v.findViewById(R.id.me_tv_personalInfo);
        mtv_personal_info.setOnClickListener(this);
        me_item_draft =v.findViewById(R.id.me_item_draft);
        me_item_draft.setOnClickListener(this);
        me_item_mine =v.findViewById(R.id.me_item_mine);
        me_item_mine.setOnClickListener(this);
        me_tv_notice = v.findViewById(R.id.me_tv_notice);
        me_tv_notice.setOnClickListener(this);
        me_tv_block = v.findViewById(R.id.me_tv_block);
        me_tv_block.setOnClickListener(this);
        h_head = v.findViewById(R.id.h_head);
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();

        if (id == R.id.btn_login) {
            Intent intent = new Intent();
            intent.setClass(requireContext(), MyLoginActivity.class);
            startActivity(intent, new Bundle());
        }
        else if (id == R.id.btn_register) {
            Intent intent = new Intent();
            intent.setClass(requireContext(), MyRegisterActivity.class);
            startActivity(intent, new Bundle());
        }
        else if (id == R.id.me_tv_personalInfo){
            Intent intent = new Intent();
            intent.setClass(requireContext(), MyOwnInfoActivity.class);
            startActivity(intent, new Bundle());
        }
        else if (id == R.id.me_item_draft){
            Intent intent = new Intent();
            intent.setClass(requireContext(), DraftBoxActivity.class);
            startActivity(intent, new Bundle());
        }
        else if (id == R.id.me_item_mine){
            Intent intent = new Intent();
            intent.setClass(requireContext(), MineBoxActivity.class);
            startActivity(intent, new Bundle());
        }
        else if (id == R.id.me_tv_notice){
            Intent intent = new Intent();
            intent.setClass(requireContext(), NoticeActivity.class);
            startActivity(intent, new Bundle());
        }
        else if (id == R.id.me_tv_block){
            Intent intent = new Intent();
            intent.setClass(requireContext(), BlockActivity.class);
            startActivity(intent, new Bundle());
        }
        else if (id == R.id.btn_log_out){
            Data.myself = null;
            btn_login.setVisibility(View.VISIBLE);
            btn_register.setVisibility(View.VISIBLE);
            btn_logout.setVisibility(View.INVISIBLE);
        }
    }

    @Override
    public String getName() {
        return Data.context.getString(R.string.t_home);
    }

    private void load (){
        if(Data.myself != null){
            btn_login.setVisibility(View.INVISIBLE);
            btn_register.setVisibility(View.INVISIBLE);
            btn_logout.setVisibility(View.VISIBLE);
        }
        else {
            btn_logout.setVisibility(View.INVISIBLE);
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        Log.d(LOG_TAG, "onResume");

        try {
            CompletableFuture.runAsync(this::load).get(); // 初始化
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onPause() {
        super.onPause();
        Log.d(LOG_TAG, "onPause");
    }
}